package org.maurange.formation.licpro.rest;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.maurange.formation.licpro.R;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import android.content.Context;
import android.util.Log;

public class InfoArretRestMethod {
	Context mContext=null;
	private String LOG_TAG = "INFO_ARRET";
	
	public InfoArretRestMethod(Context context) {
		mContext = context.getApplicationContext();
	}
	
	
	public ListInfoArret getInfoArretRest(String codeLieu){
				
		// The URL for making the GET request
		String url =  mContext.getString(R.string.info_arret_url);

		// Set the Accept header for "application/json"
		HttpHeaders requestHeaders = new HttpHeaders();
		List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
		requestHeaders.setAccept(acceptableMediaTypes);
		requestHeaders.setAcceptLanguage("fr_FR");
		
		// Populate the headers in an HttpEntity object to use for the request
		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

		// Create a new RestTemplate instance
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
		
		// Perform the HTTP GET request
		ListInfoArret data = new ListInfoArret();
		url = url+codeLieu;
		Log.d(LOG_TAG+" URL", url);
		try {
			ResponseEntity<ListInfoArret> responseEntity = restTemplate.exchange(
					url, HttpMethod.GET, requestEntity, ListInfoArret.class, "json", codeLieu);
			data.addAll(responseEntity.getBody());
		} catch (RestClientException e) {
			Log.e(LOG_TAG,"RestException dans le chargement des donn�es serveur",e);
		}
		
		return data;
	}
}
